[Rails] Accessing error_messages on form_tag

Posted by aaronrussell on Stack Overflow See other posts from Stack Overflow or by aaronrussell
Published on 2010-05-14T19:37:34Z Indexed on 2010/05/14 22:14 UTC
Read the original article Hit count: 161

I have built a custom form for creating a joining model on a has_many :through relationship. The models look roughly like this:

class Team
  has_many :team_members
  has_many :members, :through => :team_members
end

class Member
  has_many :team_members
  has_many :teams, :through => :team_members
end

class TeamMember
  belongs_to :team
  belongs_to :member
  # and this model has some validations too
end

The form I have built is for selecting which members should be in a team. I won't paste the form, but it uses the form_tag method and basically sends an array of hashes which contain a member_id and a squad_number. I then update the database with an action that looks roughly like this (simplified a bit, but you get the jist):

@team.transaction do
  @team.team_members = params[:team_members].collect{|tm| @team.team_members.new(tm)}
  if @team.save
    redirect_to ...
  else
    render :action => :members
  end
end

Everything works great but I am validating the squad_number for uniqueness and numerically. So, when any of those validations fail, how do I get access to them in my view, and how do I ascertain which of the many members it has failed on?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about forms